package com.Guuka.reggie.controller;

import com.Guuka.reggie.common.BaseContext;
import com.Guuka.reggie.common.R;
import com.Guuka.reggie.entity.Employee;
import com.Guuka.reggie.entity.User;
import com.Guuka.reggie.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 员工登录
     * @param request
     * @return employee
     * @return
     */
    @PostMapping("/login")
    public R<User> login(HttpServletRequest request, @RequestBody User user){
//        String password = user.getPassword();
//        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,user.getPhone());
        User user1 = userService.getOne(queryWrapper);

        if(user1==null){
            return R.error("用户不存在，登录失败");
        }

        if(user1.getStatus() == 0){
            return R.error("账号已禁用");
        }
        BaseContext.setCurrentId(user1.getId());

        request.getSession().setAttribute("employee",user1.getId());

        return R.success(user1);
    }
}
